package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.FunctionDAO;
import cn.edu.scau.cmi.hombio.dao.RoleAuthorityDAO;
import cn.edu.scau.cmi.hombio.dao.RoleDAO;

import cn.edu.scau.cmi.hombio.domain.Function;
import cn.edu.scau.cmi.hombio.domain.Role;
import cn.edu.scau.cmi.hombio.domain.RoleAuthority;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for RoleAuthority entities
 * 
 */

@Service("RoleAuthorityService")

@Transactional
public class RoleAuthorityServiceImpl implements RoleAuthorityService {

	/**
	 * DAO injected by Spring that manages Function entities
	 * 
	 */
	@Autowired
	private FunctionDAO functionDAO;

	/**
	 * DAO injected by Spring that manages RoleAuthority entities
	 * 
	 */
	@Autowired
	private RoleAuthorityDAO roleAuthorityDAO;

	/**
	 * DAO injected by Spring that manages Role entities
	 * 
	 */
	@Autowired
	private RoleDAO roleDAO;

	/**
	 * Instantiates a new RoleAuthorityServiceImpl.
	 *
	 */
	public RoleAuthorityServiceImpl() {
	}

	/**
	 * Delete an existing RoleAuthority entity
	 * 
	 */
	@Transactional
	public void deleteRoleAuthority(RoleAuthority roleauthority) {
		roleAuthorityDAO.remove(roleauthority);
		roleAuthorityDAO.flush();
	}

	/**
	 * Return a count of all RoleAuthority entity
	 * 
	 */
	@Transactional
	public Integer countRoleAuthoritys() {
		return ((Long) roleAuthorityDAO.createQuerySingleResult("select count(o) from RoleAuthority o").getSingleResult()).intValue();
	}

	/**
	 * Load an existing RoleAuthority entity
	 * 
	 */
	@Transactional
	public Set<RoleAuthority> loadRoleAuthoritys() {
		return roleAuthorityDAO.findAllRoleAuthoritys();
	}

	/**
	 */
	@Transactional
	public RoleAuthority findRoleAuthorityByPrimaryKey(Integer id) {
		return roleAuthorityDAO.findRoleAuthorityByPrimaryKey(id);
	}

	/**
	 * Save an existing Role entity
	 * 
	 */
	@Transactional
	public RoleAuthority saveRoleAuthorityRole(Integer id, Role related_role) {
		RoleAuthority roleauthority = roleAuthorityDAO.findRoleAuthorityByPrimaryKey(id, -1, -1);
		Role existingrole = roleDAO.findRoleByPrimaryKey(related_role.getId());

		// copy into the existing record to preserve existing relationships
		if (existingrole != null) {
			existingrole.setId(related_role.getId());
			existingrole.setName(related_role.getName());
			existingrole.setMemo(related_role.getMemo());
			related_role = existingrole;
		}

		roleauthority.setRole(related_role);
		related_role.getRoleAuthorities().add(roleauthority);
		roleauthority = roleAuthorityDAO.store(roleauthority);
		roleAuthorityDAO.flush();

		related_role = roleDAO.store(related_role);
		roleDAO.flush();

		return roleauthority;
	}

	/**
	 * Delete an existing Role entity
	 * 
	 */
	@Transactional
	public RoleAuthority deleteRoleAuthorityRole(Integer roleauthority_id, Integer related_role_id) {
		RoleAuthority roleauthority = roleAuthorityDAO.findRoleAuthorityByPrimaryKey(roleauthority_id, -1, -1);
		Role related_role = roleDAO.findRoleByPrimaryKey(related_role_id, -1, -1);

		roleauthority.setRole(null);
		related_role.getRoleAuthorities().remove(roleauthority);
		roleauthority = roleAuthorityDAO.store(roleauthority);
		roleAuthorityDAO.flush();

		related_role = roleDAO.store(related_role);
		roleDAO.flush();

		roleDAO.remove(related_role);
		roleDAO.flush();

		return roleauthority;
	}

	/**
	 * Return all RoleAuthority entity
	 * 
	 */
	@Transactional
	public List<RoleAuthority> findAllRoleAuthoritys(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<RoleAuthority>(roleAuthorityDAO.findAllRoleAuthoritys(startResult, maxRows));
	}

	/**
	 * Delete an existing Function entity
	 * 
	 */
	@Transactional
	public RoleAuthority deleteRoleAuthorityFunction(Integer roleauthority_id, Integer related_function_id) {
		RoleAuthority roleauthority = roleAuthorityDAO.findRoleAuthorityByPrimaryKey(roleauthority_id, -1, -1);
		Function related_function = functionDAO.findFunctionByPrimaryKey(related_function_id, -1, -1);

		roleauthority.setFunction(null);
		related_function.getRoleAuthorities().remove(roleauthority);
		roleauthority = roleAuthorityDAO.store(roleauthority);
		roleAuthorityDAO.flush();

		related_function = functionDAO.store(related_function);
		functionDAO.flush();

		functionDAO.remove(related_function);
		functionDAO.flush();

		return roleauthority;
	}

	/**
	 * Save an existing RoleAuthority entity
	 * 
	 */
	@Transactional
	public void saveRoleAuthority(RoleAuthority roleauthority) {
		RoleAuthority existingRoleAuthority = roleAuthorityDAO.findRoleAuthorityByPrimaryKey(roleauthority.getId());

		if (existingRoleAuthority != null) {
			if (existingRoleAuthority != roleauthority) {
				existingRoleAuthority.setId(roleauthority.getId());
				existingRoleAuthority.setAdd(roleauthority.getAdd());
				existingRoleAuthority.setDelete(roleauthority.getDelete());
				existingRoleAuthority.setCheck(roleauthority.getCheck());
				existingRoleAuthority.setEdit(roleauthority.getEdit());
				existingRoleAuthority.setMeno(roleauthority.getMeno());
			}
			roleauthority = roleAuthorityDAO.store(existingRoleAuthority);
		} else {
			roleauthority = roleAuthorityDAO.store(roleauthority);
		}
		roleAuthorityDAO.flush();
	}

	/**
	 * Save an existing Function entity
	 * 
	 */
	@Transactional
	public RoleAuthority saveRoleAuthorityFunction(Integer id, Function related_function) {
		RoleAuthority roleauthority = roleAuthorityDAO.findRoleAuthorityByPrimaryKey(id, -1, -1);
		Function existingfunction = functionDAO.findFunctionByPrimaryKey(related_function.getId());

		// copy into the existing record to preserve existing relationships
		if (existingfunction != null) {
			existingfunction.setId(related_function.getId());
			existingfunction.setName(related_function.getName());
			existingfunction.setMeno(related_function.getMeno());
			related_function = existingfunction;
		}

		roleauthority.setFunction(related_function);
		related_function.getRoleAuthorities().add(roleauthority);
		roleauthority = roleAuthorityDAO.store(roleauthority);
		roleAuthorityDAO.flush();

		related_function = functionDAO.store(related_function);
		functionDAO.flush();

		return roleauthority;
	}
}
